﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;

public class WeaponSwitchPanel : MonoBehaviour
{
    public GameObject m_WeaponDescriptionPanel;
    public GameObject m_Viewport;
    public WeaponPreviewItem m_CurrentPerviewItem;
    public GameObject m_PreviewItemPerfab;
    public Toggle m_AutoFireCB;
    public Dropdown m_GroupCMB;

    WeaponSlot m_Slot;

    private void Awake()
    {
        if (m_CurrentPerviewItem)
            m_CurrentPerviewItem.m_OnWeaponChoice = OnUnloadWeapon;
    }

    private void Update()
    {
        if (m_Slot)
        {
            RectTransform rectTransform = GetComponent<RectTransform>();

            var calcPosition = UtilsUnity.GetPositionInScreen(Camera.main.WorldToScreenPoint(m_Slot.transform.position), rectTransform.sizeDelta, UtilsUnity.Align.ALIGN_LEFT_TOP);

            float scale = 1;
            var cavas = UtilsUnity.GetCanvas();
            if (cavas&& cavas.scaleFactor>0)
                scale = 1f / cavas.scaleFactor;
            calcPosition *= scale;
            rectTransform.anchoredPosition = calcPosition;
        }
    }

    public void updateItemList(WeaponSlot slot)
    {
        m_GroupCMB.value = slot.WeaponGroup - 1;
        m_AutoFireCB.isOn = (slot.FireMode2 == WeaponSlot.FireMode.WEAPON_MODE_AUTO);
        m_Slot = slot;
        for (int i=0; i< m_Viewport.transform.childCount; ++i)
        {
            var child = m_Viewport.transform.GetChild(i);
            Destroy(child.gameObject);
        }
        VerticalLayoutGroup layout = m_Viewport.GetComponent<VerticalLayoutGroup>();

        m_CurrentPerviewItem.WeaponItem = slot.m_Weapon;


        List<Weapon> weapons = new List<Weapon>();
        foreach (var item in Player.Instance.Items)
        {
            if(item.Value != null && 
                ((item.Key > Item.ItemsType.WEAPON_BEGIN && item.Key < Item.ItemsType.WEAPON_END) || 
                (item.Key>Item.ItemsType.NETFRIEND_WEAPON_BEGINE && item.Key<Item.ItemsType.NETFRIEND_WEAPON_END)))
            {
                Weapon weapon = item.Value as Weapon;
                weapon.m_Tag =0;
                if (m_Slot.m_Level >= weapon.m_Level )
                {
                    weapon.m_Tag = 1;
                    if ((slot.m_HasAirplant && (weapon as UAV) == null) ||
                        (!slot.m_HasAirplant && (weapon as UAV) != null))
                        weapon.m_Tag = 0;
                }
                weapons.Add(weapon);
            }
        }

        float height = 0;
        for (int i=0; i< weapons.Count; ++i)
        {
            var objItem = Instantiate<GameObject>(m_PreviewItemPerfab);
            objItem.transform.SetParent(m_Viewport.transform);

            var perviewItem = objItem.GetComponent<WeaponPreviewItem>();
            perviewItem.m_WeaponDescriptionPanel = m_WeaponDescriptionPanel;
            perviewItem.WeaponItem = weapons[i];
            perviewItem.m_OnWeaponChoice = OnWeaponChoice;
            perviewItem.transform.localScale = new Vector3(1,1,1);
            height += perviewItem.GetComponent<RectTransform>().rect.height + layout.spacing;

            perviewItem.SetEnabled(weapons[i].m_Tag==1);
        }
        var viewportTransform = m_Viewport.GetComponent<RectTransform>();
        Vector2 rect = new Vector2(viewportTransform.sizeDelta.x, viewportTransform.sizeDelta.y);
        rect.y = height + layout.padding.top + layout.padding.bottom;
        viewportTransform.sizeDelta = rect;
    }

    public void SetHidden(bool hidden)
    {
        if (hidden)
        {
            if (m_GroupCMB)
                m_GroupCMB.onValueChanged.RemoveListener(OnGroupChanged);
            if (m_AutoFireCB)
                m_AutoFireCB.onValueChanged.RemoveListener(OnAutoFireChanged);
        }
        else
        {
            if (m_GroupCMB)
                m_GroupCMB.onValueChanged.AddListener(OnGroupChanged);
            if (m_AutoFireCB)
                m_AutoFireCB.onValueChanged.AddListener(OnAutoFireChanged);
        }
        gameObject.SetActive(!hidden);
    }

    void OnWeaponChoice(Weapon weapon)
    {
        if (m_Slot.m_Weapon != null)
            Player.Instance.addItem(m_Slot.m_Weapon.m_Type);
        Player.Instance.removeItem(weapon.m_Type, 1);

        m_Slot.switchWeapon(weapon);
        gameObject.SetActive(false);
    }

    void OnUnloadWeapon(Weapon weapon)
    {
        if (m_Slot.m_Weapon != null)
            Player.Instance.addItem(m_Slot.m_Weapon.m_Type);
        m_Slot.switchWeapon(null);
        gameObject.SetActive(false);
    }

    public void OnGroupChanged(int index)
    {
        if(gameObject.activeSelf)
            m_Slot.WeaponGroup = index+1;
    }

    public void OnAutoFireChanged(bool isChecked)
    {
        if(gameObject.activeSelf)
        { 
            if (isChecked)
                m_Slot.FireMode2 = WeaponSlot.FireMode.WEAPON_MODE_AUTO;
            else
                m_Slot.FireMode2 = WeaponSlot.FireMode.WEAPON_MODE_MANUAL;
        }
    }
}
